﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace Ledger
{
    public partial class LedgerDialog : Form
    {
        private List<LedgerLine> _income = new List<LedgerLine>();
        private List<LedgerLine> _expenses = new List<LedgerLine>();

        public Ledger Info
        {
            get
            {
                Ledger l = new Ledger();
                l.Expenses = this._expenses;
                l.Income = this._income;
                l.To = dateTo.Value;
                l.From = dateFrom.Value;
                l.Description = textFor.Text;
                return l;
            }
            set
            {
                this._expenses = value.Expenses;
                this._income = value.Income;
                PopulateExpenses();
                PopulateIncome();
                dateFrom.Value = value.From;
                dateTo.Value = value.To;
                textFor.Text = value.Description;
            }
        }

        private void PopulateIncome()
        {
            listIncome.Items.Clear();
            foreach (LedgerLine l in this._income)
            {
                ListViewItem lvi = new ListViewItem(l.Description);
                lvi.SubItems.Add(l.Budget.ToString("C"));
                lvi.SubItems.Add(l.ToDate.ToString("C"));
                listIncome.Items.Add(lvi);
            }
        }

        private void PopulateExpenses()
        {
            listExpenses.Items.Clear();
            foreach (LedgerLine l in this._expenses)
            {
                ListViewItem lvi = new ListViewItem(l.Description);
                lvi.SubItems.Add(l.Budget.ToString("C"));
                lvi.SubItems.Add(l.ToDate.ToString("C"));
                listExpenses.Items.Add(lvi);
            }
        }

        public LedgerDialog()
        {
            InitializeComponent();
        }

        private void toolCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
        }

        private void toolOK_Click(object sender, EventArgs e)
        {
            bool ok = true;

            if (dateFrom.Value > dateTo.Value)
            {
                ok = false;
                MessageBox.Show("The timeframe for this ledger must start before it ends.");
            }

            if (textFor.Text == "")
            {
                ok = false;
                MessageBox.Show("Please enter a description for this ledger.");
            }

            if (ok)
                this.DialogResult = System.Windows.Forms.DialogResult.OK;
        }

        private void toolIncNewLine_Click(object sender, EventArgs e)
        {
            LedgerLineDialog dlg = new LedgerLineDialog();
            if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                this._income.Add(dlg.Info);
                PopulateIncome();
            }
        }

        private void toolInEditLine_Click(object sender, EventArgs e)
        {
            int index = listIncome.SelectedIndices[0];
            if (index >= 0)
            {
                LedgerLineDialog dlg = new LedgerLineDialog();
                dlg.Info = this._income[index];
                if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    this._income[index] = dlg.Info;
                    PopulateIncome();
                }
            }
        }

        private void toolExpNewLine_Click(object sender, EventArgs e)
        {
            LedgerLineDialog dlg = new LedgerLineDialog();
            if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                this._expenses.Add(dlg.Info);
                PopulateExpenses();
            }
        }

        private void toolExpEditLine_Click(object sender, EventArgs e)
        {
            int index = listExpenses.SelectedIndices[0];
            if (index >= 0)
            {
                LedgerLineDialog dlg = new LedgerLineDialog();
                dlg.Info = this._expenses[index];
                if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    this._expenses[index] = dlg.Info;
                    PopulateExpenses();
                }
            }
        }

        private void toolReports_Click(object sender, EventArgs e)
        {
            OpenFileDialog opener = new OpenFileDialog();
            opener.Filter = "Test Files|*.txt";
            if (opener.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                string path = opener.FileName;
                StreamWriter w = new StreamWriter(path);
                w.WriteLine(textFor.Text + " " + dateFrom.Value.ToShortDateString() + "to " + dateTo.Value.ToShortDateString());
                w.WriteLine("Income");
                foreach (LedgerLine l in this._income)
                {
                    w.Write("\t" + l.Description);
                    w.Write(" Bugeted Amount: ");
                    w.Write(l.Budget.ToString("C"));
                    w.Write(" To Date: ");
                    w.WriteLine(l.ToDate.ToString("C"));
                    foreach (Transaction t in l.Transactions)
                    {
                        w.Write("\t\t" + t.Date.ToShortDateString());
                        w.Write(" " + t.Amount.ToString("C"));
                        if (t.IsRefund)
                            w.Write(" Refund ");
                        w.Write(t.Description);
                    }
                }
                w.WriteLine("Expenses");
                foreach (LedgerLine l in this._expenses)
                {
                    w.Write("\t" + l.Description);
                    w.Write(" Bugeted Amount: ");
                    w.Write(l.Budget.ToString("C"));
                    w.Write(" To Date: ");
                    w.WriteLine(l.ToDate.ToString("C"));
                    foreach (Transaction t in l.Transactions)
                    {
                        w.Write("\t\t" + t.Date.ToShortDateString());
                        w.Write(" " + t.Amount.ToString("C"));
                        if (t.IsRefund)
                            w.Write(" Refund ");
                        w.Write(t.Description);
                    }
                }
                w.Close();
            }
        }
    }
}
